//
//  Extension+AddUIBarButtonItem.swift
//  AkuSwiftSina
//
//  Created by 阿酷 on 16/7/20.
//  Copyright © 2016年 AkuApp. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    convenience init(title: String? = nil, imgName: String? = nil, target: AnyObject?, action: Selector) {

        self.init()
        
        /// 创建自定义 button
        let barButton: UIButton = UIButton()
        
        if let til = title {
            
            barButton.setTitle(til, forState: .Normal)
            barButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
            barButton.titleLabel?.font = UIFont.systemFontOfSize(15)
        }
        
        if imgName != nil {
            
            barButton.setImage(UIImage(named: imgName!), forState: .Normal)
        }
        
        // 宽高自适应
        barButton.sizeToFit()
        
        // 添加 到 自定义 button 赋值给 customView
        customView = barButton
        
        // 给Button 添加 点击事件
        barButton.addTarget(target, action: action, forControlEvents: .TouchUpInside)
    }
    
    
    
}
